import { NextResponse } from 'next/server'; import { adminApi, AdminApiError } from '@/lib/admin-api'; import { isAdmin } from '@/lib/admin-auth'; export const dynamic = 'force-dynamic'; /** Enable / disable a connector: body `{ "enabled": boolean }` → FastAPI `/admin/connectors/{name}/enabled`. */ export async function POST(req: Request, ctx: { params: Promise<{ name: string }> }): Promise { if (!(await isAdmin())) return NextResponse.json({ error: { title: 'Unauthorized', status: 401 } }, { status: 401 }); const { name } = await ctx.params; const body = (await req.json().catch(() => null)) as { enabled?: unknown } | null; if (!body || typeof body.enabled !== 'boolean') return NextResponse.json({ error: { title: 'Invalid body', detail: '`enabled` must be a boolean', status: 422 } }, { status: 422 }); try { const out = await adminApi.setEnabled(name, body.enabled); return NextResponse.json(out.data); } catch (e) { const status = e instanceof AdminApiError && e.status > 0 ? e.status : 502; return NextResponse.json({ error: { title: 'Toggle failed', detail: (e as Error).message, status } }, { status }); } }